package com.anyou.mall.pla.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.pla.domain.PlatformNotice;
import com.anyou.mall.pla.mapper.PlatformNoticeMapper;
import com.anyou.mall.pla.service.IPlatformNoticeService;
import com.anyou.mall.sho.domain.CarPartsShop;
import com.anyou.mall.sho.mapper.CarPartsShopMapper;
import com.anyou.mall.store.mapper.StoreMapper;
import com.anyou.mall.use.domain.User;
import com.anyou.mall.use.domain.UserNotice;
import com.anyou.mall.use.domain.UserSystemNotice;
import com.anyou.mall.use.mapper.UserMapper;
import com.anyou.mall.use.mapper.UserNoticeMapper;
import com.anyou.mall.use.mapper.UserSystemNoticeMapper;
import com.anyou.mall.use.service.IUserNoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 发布平台通知Service业务层处理
 *
 * @author gaochaohao
 * @date 2022-03-21
 */
@Service
public class PlatformNoticeServiceImpl implements IPlatformNoticeService
{
    @Autowired
    private PlatformNoticeMapper platformNoticeMapper;

    @Autowired
    private UserNoticeMapper userNoticeMapper;
    @Autowired
    private CarPartsShopMapper carPartsShopMapper;

    @Autowired
    private IUserNoticeService userNoticeService;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserSystemNoticeMapper userSystemNoticeMapper;

    /**
     * 查询发布平台通知
     *
     * @param platformNoticeId 发布平台通知主键
     * @return 发布平台通知
     */
    @Override
    public PlatformNotice selectPlatformNoticeByPlatformNoticeId(Long platformNoticeId)
    {
        return platformNoticeMapper.selectPlatformNoticeByPlatformNoticeId(platformNoticeId);
    }

    /**
     * 查询发布平台通知列表
     *
     * @param platformNotice 发布平台通知
     * @return 发布平台通知
     */
    @Override
    public List<PlatformNotice> selectPlatformNoticeList(PlatformNotice platformNotice)
    {
        Integer sendUserNumber = 0;
        List<PlatformNotice> platformNoticeList = platformNoticeMapper.selectPlatformNoticeList(platformNotice);
        if (platformNoticeList != null) {
            for (PlatformNotice notice : platformNoticeList) {

                List<UserNotice> userNoticeList = userNoticeService.list(new LambdaQueryWrapper<UserNotice>()
                        .eq(UserNotice::getSourceId, notice.getPlatformNoticeId())
                        .eq(UserNotice::getNoticeType,2));
                sendUserNumber += userNoticeList.toArray().length;
                notice.setSendUserNumber(Long.valueOf(sendUserNumber));
            }

        }
        return platformNoticeList;
    }

    /**
     * 新增发布平台通知
     *
     * @param platformNotice 发布平台通知
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPlatformNotice(PlatformNotice platformNotice)
    {
        platformNotice.setCreateTime(DateUtils.getNowDate());
        int i = platformNoticeMapper.insertPlatformNotice(platformNotice);
        if(i>0){
            if(platformNotice.getUserType()==1){
                //给每一个合伙人插入一条记录到合伙人消息表中
                List<CarPartsShop> carPartsShops = carPartsShopMapper.selectList(new LambdaQueryWrapper<CarPartsShop>()
                        .eq(CarPartsShop::getDelFlag, "0"));
                if(CollUtil.isNotEmpty(carPartsShops)){
                    for(CarPartsShop store : carPartsShops){
                        //UserNotice userNotice = new UserNotice();
                        //userNotice.setUserId(store.getUserId());
                        //userNotice.setNoticeType(2);
                        //userNotice.setSide(1);
                        //userNotice.setSourceId(platformNotice.getPlatformNoticeId());
                        //userNoticeMapper.insertUserNotice(userNotice);

                        UserSystemNotice userSystemNotice = new UserSystemNotice();
                        userSystemNotice.setNoticeType(3);
                        //这个字段用作公告的标题字段
                        userSystemNotice.setContent(platformNotice.getTitle());
                        userSystemNotice.setNoticeContent(platformNotice.getContent());
                        userSystemNotice.setSysUserId(store.getUserId());
                        userSystemNoticeMapper.insert(userSystemNotice);
                    }
                }
            } else if (platformNotice.getUserType() == 0) {
                //将通知消息的编号和用户编号插入到用户消息通知表中
                List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>()
                        .eq(User::getDelFlag, "0"));
                if(CollUtil.isNotEmpty(users)){
                    for(User user : users){
                        UserNotice userNotice = new UserNotice();
                        userNotice.setUserId(user.getUserId());
                        userNotice.setNoticeType(2);
                        userNotice.setSide(0);
                        userNotice.setSourceId(platformNotice.getPlatformNoticeId());
                        userNoticeMapper.insert(userNotice);
                    }

                }
            } else if(platformNotice.getUserType() == 2){
                //将通知消息的编号和用户编号插入到用户消息通知表中
                List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>()
                        .eq(User::getDelFlag, "0"));
                if(CollUtil.isNotEmpty(users)){
                    for(User user : users){
                        UserNotice userNotice = new UserNotice();
                        userNotice.setUserId(user.getUserId());
                        userNotice.setNoticeType(2);
                        userNotice.setSide(0);
                        userNotice.setSourceId(platformNotice.getPlatformNoticeId());
                        userNoticeMapper.insert(userNotice);
                    }

                }

                //给每一个合伙人插入一条记录到合伙人消息表中
                List<CarPartsShop> carPartsShops = carPartsShopMapper.selectList(new LambdaQueryWrapper<CarPartsShop>()
                        .eq(CarPartsShop::getDelFlag, "0"));
                if(CollUtil.isNotEmpty(carPartsShops)){
                    for(CarPartsShop store : carPartsShops){
                        //UserNotice userNotice = new UserNotice();
                        //userNotice.setUserId(store.getUserId());
                        //userNotice.setNoticeType(2);
                        //userNotice.setSide(1);
                        //userNotice.setSourceId(platformNotice.getPlatformNoticeId());
                        //userNoticeMapper.insertUserNotice(userNotice);

                        UserSystemNotice userSystemNotice = new UserSystemNotice();
                        userSystemNotice.setNoticeType(3);
                        //这个字段用作公告的标题字段
                        userSystemNotice.setContent(platformNotice.getTitle());
                        userSystemNotice.setNoticeContent(platformNotice.getContent());
                        userSystemNotice.setSysUserId(store.getUserId());
                        userSystemNotice.setSourceId(platformNotice.getPlatformNoticeId());
                        userSystemNoticeMapper.insert(userSystemNotice);
                    }
                }
            }
        }
        return i;
    }

    /**
     * 修改发布平台通知
     *
     * @param platformNotice 发布平台通知
     * @return 结果
     */
    @Override
    public int updatePlatformNotice(PlatformNotice platformNotice)
    {
        platformNotice.setUpdateTime(DateUtils.getNowDate());
        return platformNoticeMapper.updatePlatformNotice(platformNotice);
    }

    /**
     * 批量删除发布平台通知
     *
     * @param platformNoticeIds 需要删除的发布平台通知主键
     * @return 结果
     */
    @Override
    public int deletePlatformNoticeByPlatformNoticeIds(Long[] platformNoticeIds)
    {
        if (platformNoticeIds != null && platformNoticeIds.length != 0) {
            for (Long platformNoticeId : platformNoticeIds) {
                // 将use_user_notice中的数据删除
                userNoticeMapper.delete(new LambdaQueryWrapper<UserNotice>()
                        .eq(UserNotice::getSourceId,platformNoticeId)
                        .eq(UserNotice::getNoticeType,2)
                );
            }
        }
        return platformNoticeMapper.deletePlatformNoticeByPlatformNoticeIds(platformNoticeIds);
    }

    /**
     * 删除发布平台通知信息
     *
     * @param platformNoticeId 发布平台通知主键
     * @return 结果
     */
    @Override
    public int deletePlatformNoticeByPlatformNoticeId(Long platformNoticeId)
    {
        return platformNoticeMapper.deletePlatformNoticeByPlatformNoticeId(platformNoticeId);
    }

    @Override
    public ApiResult withdrawPlatformNotice(Long platformNoticeId) {
        PlatformNotice platformNotice = platformNoticeMapper.selectPlatformNoticeByPlatformNoticeId(platformNoticeId);
        if (platformNotice != null) {
            int i = platformNoticeMapper.deletePlatformNoticeByPlatformNoticeId(platformNoticeId);
            // 删除合伙人表中的消息
            userSystemNoticeMapper.deleteUserSystemNoticeByPlatformNoticeId(platformNoticeId);
            // 删除用户消息通知表中的信息
            userNoticeService.deleteDataByPlatformNoticeId(platformNoticeId);
            return ApiResult.success();
        } else {
            return ApiResult.error("要撤回的通知不存在");
        }
    }
}
